//
//  ViewController.m
//  UI03_UI
//
//  Created by LGC on 16/3/11.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "LTView.h"
#import "MyButton.h"
#import "VCOfSecond.h"
// 延展
@interface ViewController () <UITextFieldDelegate >

@end

@implementation ViewController

/** 初始化 */

- (instancetype)init {
    self = [super init];
    if (self) {
        NSLog(@"%s, %d", __FUNCTION__, __LINE__ );
    }
    return self;
}

/** 首先从图形化(nib)进行初始化, 如果没有nib文件, 尝试从代码初始化.所以无论是图形化还是代码进入, 都会调用这个初始化方法.
    所以通常重写controller的初始化都是重写这个方法. */

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"%s, %d", __FUNCTION__, __LINE__ );
    }
    return self;
    
}

/** 加载视图 */

- (void)loadView {
    [super loadView];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
}

/** 视图加载完毕 
 * 通常, 代码是在此方法编写.
 */
- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
    [self createSubviews];
    
}

#pragma mark - 创建自定义视图.
- (void)createSubviews {
    // 用户名
    LTView *viewOfUser = [[LTView alloc]initWithFrame:CGRectMake(40, 50, self.view.frame.size.width - 80, 40) Text:@"用户名:" placeHolder:@"请输入用户名"];
    
    [self.view addSubview:viewOfUser];
    
    [viewOfUser release];
    
    
    viewOfUser.textFieldOfRight.delegate = self;
    
    // 密码
    LTView *viewOfPassword = [[LTView alloc]initWithFrame:CGRectMake(40, 110, self.view.frame.size.width - 80, 40) Text:@"密码:" placeHolder:@"请输入密码"];
    
    [self.view addSubview:viewOfPassword];
    
    [viewOfPassword release];
    
    viewOfPassword.textFieldOfRight.delegate = self;
    
    // 确定Button
    
    MyButton *buttonOfConfirm = [MyButton buttonWithType:UIButtonTypeSystem];
    buttonOfConfirm.frame = CGRectMake(80, 220, 100, 40);
    [buttonOfConfirm setTitle:@"确定" forState:UIControlStateNormal];
    [self.view addSubview:buttonOfConfirm];
    
    // 注册Button
    MyButton *buttonOfReg = [MyButton buttonWithType:UIButtonTypeSystem];
    buttonOfReg.frame = CGRectMake(200, 220, 100, 40);
    [buttonOfReg setTitle:@"注册" forState:UIControlStateNormal];
    [self.view addSubview:buttonOfReg];
    
    [buttonOfReg addTarget:self action:@selector(handleReg:) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - 跳转下一页面.
- (void)handleReg:(MyButton *)button {
    
    // 创建第二页面对象.
    
    VCOfSecond *second = [[VCOfSecond alloc]init];
    
    // 模态推出第二界面.(controller方法)
    
    [self presentViewController:second animated:YES completion:^{
        
    }];
    
    [second release];
    
}

#pragma mark - 实现delegate方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
    
}


/** 视图将要显示 */
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
}

/** 视图已经显示 */
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}

/** 视图将要消失 */
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}

/** 视图已经消失 */
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}


/** 内存不足时, 调用此方法. */
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}

@end
